package com.company.clientscard.server.entityimport;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.company.clientscard.server.domain.Client;
import com.company.clientscard.server.service.ClientController;
import com.company.clientscard.server.service.DocumentController;

public class DocumentEntityImporter {
	private String fileName;

	final DocumentController dc = new DocumentController();
	final ClientController cc = new ClientController();

	public void setAttributesToObject(Element element, Object object,
			Client client) {
		com.company.clientscard.server.domain.Document doc;

		boolean update = false;

		if (object == null) {
			doc = new com.company.clientscard.server.domain.Document();
		} else {
			doc = (com.company.clientscard.server.domain.Document) object;
			update = true;
		}

		doc.setClient(client);

		for (Iterator i = element.attributeIterator(); i.hasNext();) {
			Attribute attribute = (Attribute) i.next();

			Field[] fields = doc.getClass().getDeclaredFields();
			List<Field> list = Arrays.asList(fields);
			for (Field field : list) {
				if (field.getName().equals(attribute.getName())) {

					try {
						String attName = attribute.getName();
						String firstC = attName.substring(0, 1).toUpperCase();
						String methodName = "set" + firstC
								+ attName.substring(1);

						Object value = attribute.getValue();

						Class<?> clazz = String.class;
						if (field.getType().equals(Date.class)) {
							clazz = Date.class;
							DateFormat dateFormat = new SimpleDateFormat(
									"dd.MM.yyyy");
							try {
								value = dateFormat.parse((String) value);
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						} else if (field.getType().equals(Client.class)) {
							clazz = Client.class;
							value = client;

							System.out.println("-------------Client: "
									+ client.getName());
						}

						doc.getClass().getDeclaredMethod(methodName, clazz)
								.invoke(doc, value);
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

		if (update) {
			dc.update(doc);
		} else {
			dc.save(doc);
		}
	}

	public DocumentEntityImporter(String fileName) {
		this.fileName = fileName;
		File baseDir = new File("D:\\java\\importToDb");

		File fileToImport = new File(baseDir, fileName);
		SAXReader saxReader = new SAXReader();

		Attribute attribute;

		try {
			Document document = saxReader.read(fileToImport);

			Element root = document.getRootElement();
			// iterate through child elements of root
			for (Iterator i = root.elementIterator(); i.hasNext();) {
				Element element = (Element) i.next();

				attribute = element.attribute("clientCode");
				if (attribute == null) {
					continue;
				}

				String clientCode = attribute.getValue();

				List<Client> clientList = cc.findWithParam("code", clientCode);

				Client client;
				if (clientList.size() == 0) {
					continue;
				} else {
					client = clientList.get(0);
					if (client == null) {
						continue;
					}
				}

				attribute = element.attribute("id1c");
				if (attribute == null) {
					continue;
				}

				String id1c = attribute.getValue();

				List<com.company.clientscard.server.domain.Document> list = dc
						.findWithParam("id1c", id1c);

				if (list.size() == 0) {
					setAttributesToObject(element, null, client);
				} else {
					setAttributesToObject(element, list.get(0), client);
				}

			}

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
